package com.kingdee.webapi.javasdk.purpurchaseorder;

import static org.junit.Assert.fail;

import java.time.Duration;
import java.util.*;
import java.util.concurrent.TimeUnit;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.gson.JsonObject;
import com.kingdee.bos.webapi.entity.*;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
import com.kingdee.webapi.javasdk.common.HttpRequestUtil;
import com.kingdee.webapi.javasdk.common.SeqHelper;
import org.apache.commons.codec.binary.StringUtils;
import org.junit.FixMethodOrder;
import org.junit.Test;
import com.google.gson.Gson;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class   PurPurchaseOrderTest {
    K3CloudApi api = new K3CloudApi();
    static String FNumber = SeqHelper.genNumber("CGDD");
    static String FName = "aucgdd_" + UUID.randomUUID().toString();

    //氚云辅料采购单单据编码
    static String schemaCodeCYFLCG = "D279357Sh1hjskmjufjwh8e3volp6rud7";

    static Cache<String, String> cache = Caffeine.newBuilder().maximumSize(10000).expireAfterWrite(48, TimeUnit.HOURS).build();

    String json = "{\n" +
            "  \"NeedUpDateFields\": [],\n" +
            "  \"NeedReturnFields\": [],\n" +
            "  \"IsDeleteEntry\": \"true\",\n" +
            "  \"SubSystemId\": \"\",\n" +
            "  \"IsVerifyBaseDataField\": \"false\",\n" +
            "  \"IsEntryBatchFill\": \"true\",\n" +
            "  \"ValidateFlag\": \"true\",\n" +
            "  \"NumberSearch\": \"true\",\n" +
            "  \"IsAutoAdjustField\": \"false\",\n" +
            "  \"InterationFlags\": \"\",\n" +
            "  \"IgnoreInterationFlag\": \"\",\n" +
            "  \"Model\": {\n" +
            "    \"FBillTypeID\": {\n" +
            "      \"FNUMBER\": \"CGDD01_SYS\"\n" +
            "    },\n" +
            "    \"FDate\": \"2022-04-25 00:00:00\",\n" +
            "    \"FSupplierId\": {\n" +
            "      \"FNumber\": \"SCMGYS000002\"\n" +
            "    },\n" +
            "    \"FPurchaseOrgId\": {\n" +
            "      \"FNumber\": \"100\"\n" +
            "    },\n" +
            "    \"FPurchaseDeptId\": {\n" +
            "      \"FNumber\": \"FINBM000200\"\n" +
            "    },\n" +
            "    \"FPurchaserGroupId\": {\n" +
            "      \"FNumber\": \"FINYWZ000203\"\n" +
            "    },\n" +
            "    \"FPurchaserId\": {\n" +
            "      \"FNumber\": \"FINYG000202\"\n" +
            "    },\n" +
            "    \"FProviderId\": {\n" +
            "      \"FNumber\": \"SCMGYS000002\"\n" +
            "    },\n" +
            "    \"FSettleId\": {\n" +
            "      \"FNumber\": \"SCMGYS000002\"\n" +
            "    },\n" +
            "    \"FChargeId\": {\n" +
            "      \"FNumber\": \"SCMGYS000002\"\n" +
            "    },\n" +
            "    \"FChangeStatus\": \"A\",\n" +
            "    \"FACCTYPE\": \"Q\",\n" +
            "    \"FPOOrderFinance\": {\n" +
            "      \"FSettleModeId\": {\n" +
            "        \"FNumber\": \"JSFS01_SYS\"\n" +
            "      },\n" +
            "      \"FPayConditionId\": {\n" +
            "        \"FNumber\": \"FKTJ01_SYS\"\n" +
            "      },\n" +
            "      \"FSettleCurrId\": {\n" +
            "        \"FNumber\": \"PRE001\"\n" +
            "      },\n" +
            "      \"FExchangeTypeId\": {\n" +
            "        \"FNumber\": \"HLTX01_SYS\"\n" +
            "      },\n" +
            "      \"FExchangeRate\": 1,\n" +
            "      \"FPriceTimePoint\": \"1\",\n" +
            "      \"FFOCUSSETTLEORGID\": {\n" +
            "        \"FNumber\": \"100\"\n" +
            "      },\n" +
            "      \"FIsIncludedTax\": true,\n" +
            "      \"FISPRICEEXCLUDETAX\": true,\n" +
            "      \"FLocalCurrId\": {\n" +
            "        \"FNumber\": \"PRE001\"\n" +
            "      },\n" +
            "      \"FSupToOderExchangeBusRate\": 1\n" +
            "    },\n" +
            "    \"FPOOrderEntry\": [\n" +
            "      {\n" +
            "        \"FProductType\": \"1\",\n" +
            "        \"FMaterialId\": {\n" +
            "          \"FNumber\": \"SCMWL000012\"\n" +
            "        },\n" +
            "        \"FMaterialDesc\": \"SCMWL000012采购(可退物料)\",\n" +
            "        \"FUnitId\": {\n" +
            "          \"FNumber\": \"Pcs\"\n" +
            "        },\n" +
            "        \"FQty\": 6,\n" +
            "        \"FPriceUnitId\": {\n" +
            "          \"FNumber\": \"Pcs\"\n" +
            "        },\n" +
            "        \"FPriceUnitQty\": 6,\n" +
            "        \"FPriceBaseQty\": 6,\n" +
            "        \"FDeliveryDate\": \"2022-04-25 15:03:00\",\n" +
            "        \"FPrice\": 796.460177,\n" +
            "        \"FTaxPrice\": 900,\n" +
            "        \"FEntryTaxRate\": 13,\n" +
            "        \"FRequireOrgId\": {\n" +
            "          \"FNumber\": \"100\"\n" +
            "        },\n" +
            "        \"FRequireDeptId\": {\n" +
            "          \"FNumber\": \"SCMBM000001\"\n" +
            "        },\n" +
            "        \"FReceiveOrgId\": {\n" +
            "          \"FNumber\": \"100\"\n" +
            "        },\n" +
            "        \"FEntrySettleOrgId\": {\n" +
            "          \"FNumber\": \"100\"\n" +
            "        },\n" +
            "        \"FStockUnitID\": {\n" +
            "          \"FNumber\": \"Pcs\"\n" +
            "        },\n" +
            "        \"FStockQty\": 6,\n" +
            "        \"FStockBaseQty\": 6,\n" +
            "        \"FSupplierLot\": \"11111111111\",\n" +
            "        \"FDeliveryMaxQty\": 6,\n" +
            "        \"FDeliveryMinQty\": 6,\n" +
            "        \"FDeliveryEarlyDate\": \"2022-04-25 15:03:00\",\n" +
            "        \"FDeliveryLastDate\": \"2022-04-25 15:03:00\",\n" +
            "        \"FPriceCoefficient\": 1,\n" +
            "        \"FEntrySettleModeId\": {\n" +
            "          \"FNumber\": \"JSFS01_SYS\"\n" +
            "        },\n" +
            "        \"FReqTraceNo\": \"1111111111\",\n" +
            "        \"FPlanConfirm\": true,\n" +
            "        \"FSalUnitID\": {\n" +
            "          \"FNumber\": \"Pcs\"\n" +
            "        },\n" +
            "        \"FSalQty\": 6,\n" +
            "        \"FCentSettleOrgId\": {\n" +
            "          \"FNumber\": \"100\"\n" +
            "        },\n" +
            "        \"FDispSettleOrgId\": {\n" +
            "          \"FNumber\": \"100\"\n" +
            "        },\n" +
            "        \"FDeliveryStockStatus\": {\n" +
            "          \"FNumber\": \"KCZT02_SYS\"\n" +
            "        },\n" +
            "        \"FSalBaseQty\": 6,\n" +
            "        \"FEntryPayOrgId\": {\n" +
            "          \"FNumber\": \"100\"\n" +
            "        },\n" +
            "        \"FAllAmountExceptDisCount\": 5400,\n" +
            "        \"FEntryDeliveryPlan\": [\n" +
            "          {\n" +
            "            \"FDeliveryDate_Plan\": \"2022-04-25 15:03:00\",\n" +
            "            \"FPlanQty\": 6,\n" +
            "            \"FPREARRIVALDATE\": \"2022-04-25 15:03:00\"\n" +
            "          }\n" +
            "        ]\n" +
            "      },\n" +
            "      {\n" +
            "        \"FProductType\": \"1\",\n" +
            "        \"FMaterialId\": {\n" +
            "          \"FNumber\": \"SCMWL000012\"\n" +
            "        },\n" +
            "        \"FMaterialDesc\": \"SCMWL000012采购(可退物料)\",\n" +
            "        \"FUnitId\": {\n" +
            "          \"FNumber\": \"Pcs\"\n" +
            "        },\n" +
            "        \"FQty\": 6,\n" +
            "        \"FPriceUnitId\": {\n" +
            "          \"FNumber\": \"Pcs\"\n" +
            "        },\n" +
            "        \"FPriceUnitQty\": 6,\n" +
            "        \"FPriceBaseQty\": 6,\n" +
            "        \"FDeliveryDate\": \"2022-04-25 15:03:18\",\n" +
            "        \"FPrice\": 796.460177,\n" +
            "        \"FTaxPrice\": 900,\n" +
            "        \"FEntryTaxRate\": 13,\n" +
            "        \"FRequireOrgId\": {\n" +
            "          \"FNumber\": \"100\"\n" +
            "        },\n" +
            "        \"FReceiveOrgId\": {\n" +
            "          \"FNumber\": \"100\"\n" +
            "        },\n" +
            "        \"FEntrySettleOrgId\": {\n" +
            "          \"FNumber\": \"100\"\n" +
            "        },\n" +
            "        \"FStockUnitID\": {\n" +
            "          \"FNumber\": \"Pcs\"\n" +
            "        },\n" +
            "        \"FStockQty\": 6,\n" +
            "        \"FStockBaseQty\": 6,\n" +
            "        \"FDeliveryMaxQty\": 6,\n" +
            "        \"FDeliveryMinQty\": 6,\n" +
            "        \"FDeliveryEarlyDate\": \"2022-04-25 15:03:18\",\n" +
            "        \"FDeliveryLastDate\": \"2022-04-25 15:03:18\",\n" +
            "        \"FPriceCoefficient\": 1,\n" +
            "        \"FEntrySettleModeId\": {\n" +
            "          \"FNumber\": \"JSFS01_SYS\"\n" +
            "        },\n" +
            "        \"FPlanConfirm\": true,\n" +
            "        \"FSalUnitID\": {\n" +
            "          \"FNumber\": \"Pcs\"\n" +
            "        },\n" +
            "        \"FSalQty\": 6,\n" +
            "        \"FCentSettleOrgId\": {\n" +
            "          \"FNumber\": \"100\"\n" +
            "        },\n" +
            "        \"FDispSettleOrgId\": {\n" +
            "          \"FNumber\": \"100\"\n" +
            "        },\n" +
            "        \"FDeliveryStockStatus\": {\n" +
            "          \"FNumber\": \"KCZT02_SYS\"\n" +
            "        },\n" +
            "        \"FSalBaseQty\": 6,\n" +
            "        \"FEntryPayOrgId\": {\n" +
            "          \"FNumber\": \"100\"\n" +
            "        },\n" +
            "        \"FAllAmountExceptDisCount\": 5400,\n" +
            "        \"FEntryDeliveryPlan\": [\n" +
            "          {\n" +
            "            \"FDeliveryDate_Plan\": \"2022-04-25 15:03:18\",\n" +
            "            \"FPlanQty\": 6,\n" +
            "            \"FSUPPLIERDELIVERYDATE\": \"2022-04-25 15:03:18\",\n" +
            "            \"FPREARRIVALDATE\": \"2022-04-25 15:03:18\"\n" +
            "          }\n" +
            "        ]\n" +
            "      }\n" +
            "    ],\n" +
            "    \"FIinstallment\": [\n" +
            "      {\n" +
            "        \"FYFRATIO\": 100,\n" +
            "        \"FYFAMOUNT\": 10800\n" +
            "      }\n" +
            "    ],\n" +
            "    \"FBillNo\": "+"\""+FNumber+"\""+",\n" +
            "  },\n" +
            "  \"TargetFormId\": \"0\"\n" +
            "}";


    /* 本接口用于实现采购订单 (PUR_PurchaseOrder)的保存功能 */
    @Test
    public void atestSavePurchaseOrder() throws Exception {
        String result = api.save("PUR_PurchaseOrder", json);
        Gson gson = new Gson();
        RepoRet sRet = gson.fromJson(result, RepoRet.class);
        if (sRet.isSuccessfully()) {

            System.out.printf("%s%n", gson.toJson(sRet.getResult()));
        } else {
            fail("采购订单保存接口: " + gson.toJson(sRet.getResult()));
        }
    }


    /*本接口用于实现采购订单 (PUR_PurchaseOrder)的提交功能*/
    @Test
    public void btestSubmitPurchaseOrder() throws Exception {
        JsonObject jsonData = new JsonObject();
        jsonData.addProperty("Numbers", FNumber);
        String result = api.submit("PUR_PurchaseOrder", jsonData.toString());
        Gson gson = new Gson();
        RepoRet repoRet = gson.fromJson(result, RepoRet.class);
        if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
            System.out.printf("采购订单提交接口: %s%n", gson.toJson(repoRet.getResult()));
        } else {
            fail("采购订单提交接口: " + gson.toJson(repoRet.getResult()));
        }

    }

    /*本接口用于实现采购订单 (PUR_PurchaseOrder)的审核功能*/
    @Test
    public void ctestauditPurchaseOrder() throws Exception {
        JsonObject jsonData = new JsonObject();
        jsonData.addProperty("Numbers", FNumber);
        String result = api.audit("PUR_PurchaseOrder", jsonData.toString());
        Gson gson = new Gson();
        RepoRet repoRet = gson.fromJson(result, RepoRet.class);
        if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
            System.out.printf("采购订单审核接口: %s%n", gson.toJson(repoRet.getResult()));
        } else {
            fail("采购订单审核接口: " + gson.toJson(repoRet.getResult()));
        }

    }

    /*本接口用于实现采购订单 (PUR_PurchaseOrder)的反审核功能*/
    @Test
    public void dtestunauditPurchaseOrder() throws Exception {
        JsonObject jsonData = new JsonObject();
        jsonData.addProperty("Numbers", FNumber);
        String result = api.unAudit("PUR_PurchaseOrder", jsonData.toString());
        Gson gson = new Gson();
        RepoRet repoRet = gson.fromJson(result, RepoRet.class);
        if (repoRet.getResult().getResponseStatus().isIsSuccess()) {

            System.out.printf("采购订单反审核接口: %s%n", gson.toJson(repoRet.getResult()));
        } else {
            fail("采购订单反审核接口: " + gson.toJson(repoRet.getResult().getResponseStatus()));
        }
    }



    /*本接口用于实现采购订单 (PUR_PurchaseOrder)的查看功能*/
    @Test
    public void gtestviewPurchaseOrder() throws Exception {
        JsonObject jsonData = new JsonObject();
        jsonData.addProperty("Number", FNumber);
        String result = api.view("PUR_PurchaseOrder", jsonData.toString());
        Gson gson = new Gson();
        RepoRet repoRet = gson.fromJson(result, RepoRet.class);
        if (repoRet.getResult().getResponseStatus().isIsSuccess()) {

            System.out.printf("采购订单查看接口: %s%n", result);
        } else {
            fail("采购订单查看接口: " + result);
        }
    }

    public static void main(String[] args) {
        // 创建Caffeine缓存对象
        // 设置缓存最大数量
        // 设置过期时间
//        Cache<String, String> cache = Caffeine.newBuilder().maximumSize(100).expireAfterWrite(48, TimeUnit.HOURS).build();
        // 添加缓存项
//        cache.put("key", "value");
//        // 获取缓存项
//        String value = cache.getIfPresent("key");



        PurPurchaseOrderTest ppo = new PurPurchaseOrderTest();
        final int[] i = {0};
        TimerTask task = new TimerTask() {
            public void run() {
                try {
                    // 在此处编写需要定时执行的代码
//                    ppo.htestQueryPurchaseOrder();
                    System.out.println("定时器处理");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println("Hello " + i[0]++ + ", I'm a scheduled task!");

//                ppo.createCYData();
            }
        };

        Timer timer = new Timer();
        timer.schedule(task, 0, 15000); // 第二个参数为延迟时间，第三个参数为间隔时间
    }

    /**
     * 预处理采购订单，用于获取待同步订单清单
     * 查询金蝶修改日期为当日且审核通过的订单清单，然后在氚云检测是否已有，返回氚云没有的订单清单
     */
    @Test
    public void preprocessCGDDList() throws Exception {
        //0-6 供应商, 采购日期, 单据编号, 客户合同号, 朔翔号（订单号）, 单据提内码
        // 最后： FModifyDate
        // [[101527, 2023-07-06T00:00:00, 0, CGDD007360, null, 1TSGW8, null, 270048.0, 张 结算, 2023-07-10T00:00:00, 289139, 2023-07-05T17:54:50.953, 107592, B]]
        String fieldstr = "FSupplierId.Fname,FDate,FBillNo, FMtoNo, F_qwe_Text4, FPOOrderEntry_FEntryID, " +
                "FModifyDate";
        String modifyDate = "2023-7-7";
        String json = "{\n" +
                "    \"FormId\": \"PUR_PurchaseOrder\",\n" +
                "    \"FieldKeys\": \"" + fieldstr + "\",\n" +
                "    \"FilterString\": \"FModifyDate>=\'" + modifyDate + "\' and FDocumentStatus=\'C\'\",\n" +
                "    \"OrderString\": \"\",\n" +
                "    \"TopRowCount\": 0,\n" +
                "    \"StartRow\": 0,\n" +
                "    \"Limit\": 2000,\n" +
                "    \"SubSystemId\": \"\"\n" +
                "}";
        try {
            String result = String.valueOf(api.executeBillQuery(json));
            if (result.length() > 2) {
                String[][] formatResult = formatJdyData(result);
                for (int i=0; i<formatResult.length; i++) {
                    String billNoEntryId = formatResult[i][2] + "-" + formatResult[i][5];
                    if (cache.getIfPresent(billNoEntryId) != null) {
                        continue;
                    } else {
                        // 查询氚云，如存在则写入cache后，continue
                        String strFilter = "{\"FromRowNum\":   0,\"RequireCount\": false,\"ReturnItems\": [],   " +
                                "\"SortByCollection\": [],\"ToRowNum\": 500,   \"Matcher\":{\"Type\":\"And\",   \"Matchers\":[{\"Type\":\"And\"," +
                                "\"Matchers\":[{\"Type\":\"Item\",\"Name\":\"F0000013\",\"Operator\":2,\"Value\":\"CGDD000056\"}]}]}}";
//                        String strFilter = "{\"FromRowNum\":0,\"RequireCount\":false,\"ReturnItems\":["ObjectId", "F0000039", "F0000013"],\"SortByCollection\":[],\"ToRowNum\":500,\"Matcher\":{\"Type\":\"And\",\"Matchers\":[{\"Type\":\"Item\",\"Name\":\"F0000039\",\"Operator\":2,\"Value\":\"CGDD007035-119130\"}]}}";
                        String reData = queryCYDatas(schemaCodeCYFLCG, strFilter);
                        if (reData.contains("{\"Successful\":true,")) {
                            if (reData.length() > 110) {
                                System.out.println("该单号已存在: ");
                                cache.put(billNoEntryId, "存在");
                            } else {
                                // 尚不存在，同步到氚云

                            }
                        }
                        // 如不存在则写入氚云
                    }
                }
                System.out.println("formatResult: " + formatResult);
            }
            System.out.println("采购订单单据查询接口: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 金蝶采购订单 To 氚云辅料采购单
     * 用于历史数据批量迁移
     * @throws Exception
     */
    @Test
    public void syncJdyData2Cy()  throws Exception{
        String gdStr = "CGDD";
        String bdStr = "";
        for (int i = 7200; i<=7400; i++) {
            System.out.println("i = " + i);
            bdStr = gdStr + String.format("%06d", i);
            htestQueryPurchaseOrder(bdStr);
        }
    }

    /*本接口用于实现采购订单 (PUR_PurchaseOrder)的单据查询功能*/
//    @Test
    public void htestQueryPurchaseOrder(String FNumber) throws Exception {
//        FNumber = "CGDD007358";
        //0-5 供应商, 采购日期, 采购员, 单据编号, 采购订单号
        //5-5 客户合同号, 物料名称, 采购数量, 规格型号, 交货日期
        //10-4 补充说明备注, 创建人, 创建日期, 产品名称
        //14-6 实体主键, 单据状态, 朔翔号（订单号）, 备注1（辅料规格; 先占位）, 对应产品（产品名称）, 跟单员名称（业务员）
        //20 纸箱纸质要求（F_QWE_TEXT）, 使用说明（F_QWE_TEXT2）, 备注（FEntryNote）,
        // 最后： FModifyDate  ($$FAUXPROPID__FF100002,)
        // [[101527, 2023-07-06T00:00:00, 0, CGDD007360, null, 1TSGW8, null, 270048.0, 张 结算, 2023-07-10T00:00:00, 289139, 2023-07-05T17:54:50.953, 107592, B]]
        String fieldstr = "FSupplierId.Fname,FDate,FPurchaserId.Fname,FBillNo,FPURCHASEORDERNO, " +
                "FMtoNo,FMaterialName,FQty,FModel,FDeliveryDate," +
                "F_qwe_Text1, FCreatorId.fname,FCreateDate, F_qwe_Base, " +
                "FID,FDocumentStatus, F_qwe_Text4, FAUXPROPID.FF100002, F_QWE_BASE1.fname, F_qwe_Text6," +
                "F_QWE_TEXT, F_QWE_TEXT2, FEntryNote,  " +
                "FModifyDate";
        String json = "{\n" +
                "    \"FormId\": \"PUR_PurchaseOrder\",\n" +
                "    \"FieldKeys\": \"" + fieldstr + "\",\n" +
                "    \"FilterString\": \"FBillNo=\'" + FNumber + "\' and FDocumentStatus=\'C\'\",\n" +
                "    \"OrderString\": \"\",\n" +
                "    \"TopRowCount\": 0,\n" +
                "    \"StartRow\": 0,\n" +
                "    \"Limit\": 2000,\n" +
                "    \"SubSystemId\": \"\"\n" +
                "}";
        String result = String.valueOf(api.executeBillQuery(json));
        if (result.length() > 2) {
            String[][] formatResult = formatJdyData(result);
            for (int i=0; i<formatResult.length; i++) {
                createCYData(formatCyData(formatResult[i]));
            }
            System.out.println("formatResult: " + formatResult);
        }
        System.out.println("采购订单单据查询接口: " + result);
    }

    public String formatCyData(String[] jdyStr) {
        //0-5 供应商, 采购日期, 采购员, 单据编号, 采购订单号
        //5-5 客户合同号, 物料名称, 采购数量, 规格型号, 交货日期
        //10-4 补充说明备注, 创建人, 创建日期, 产品名称
        //14-6 实体主键, 单据状态, 朔翔号（订单号）, 备注1（辅料规格; 先占位）, 对应产品（产品名称）, 跟单员名称（业务员）
        //20 纸箱纸质要求（F_QWE_TEXT）, 使用说明（F_QWE_TEXT2）, 备注（FEntryNote）,
        // 最后： FModifyDate

        String cyData = "{\"F0000029\":\"供应商\",\"F0000031\":\"采购日期\",\"F0000035\":\"业务员（文本）\",\"F0000013\":\"采购订单号\"," +
                "\"F0000026\":\"客户合同号\",\"F0000017\":\"产品名称\",\"F0000021\":\"物料名称\",\"F0000024\":采购数量,\"F0000022\":\"辅料规格\",\"F0000028\":\"交货日期\"," +
                "\"F0000030\":\"补充备注说明\",\"F0000033\":\"创建人（文本）\",\"CreatedBy\":\"858c3e2a-b79f-410e-96eb-8e74c3b526d8\",\"CreatedTime\":\"2023/7/7 16:16:16\",\"OwnerId\":\"858c3e2a-b79f-410e-96eb-8e74c3b526d8\"," +
                "\"F0000034\":\"订单号\",\"F0000036\":\"纸箱纸质要求\", \"F0000037\":\"使用说明\", \"F0000038\":\"99备注99\"}";
        cyData = cyData.replaceAll("供应商", jdyStr[0]);
        cyData = cyData.replaceAll("采购日期", jdyStr[1]);
        cyData = cyData.replaceAll("业务员（文本）", jdyStr[19]);
        cyData = cyData.replaceAll("采购订单号", jdyStr[3]);

        cyData = cyData.replaceAll("客户合同号", jdyStr[5]);
        cyData = cyData.replaceAll("物料名称", jdyStr[6]);
        cyData = cyData.replaceAll("采购数量", jdyStr[7]);
        cyData = cyData.replaceAll("辅料规格", jdyStr[17]);
        cyData = cyData.replaceAll("交货日期", jdyStr[9]);

        cyData = cyData.replaceAll("补充备注说明", jdyStr[10]);
        cyData = cyData.replaceAll("创建人（文本）", jdyStr[11]);
        cyData = cyData.replaceAll("产品名称", jdyStr[18]);
        cyData = cyData.replaceAll("订单号", jdyStr[16]);
        cyData = cyData.replaceAll("纸箱纸质要求", jdyStr[20]);

        cyData = cyData.replaceAll("使用说明", jdyStr[21]);
        cyData = cyData.replaceAll("99备注99", jdyStr[22]);
        System.out.println("cyData: " + cyData);
        return cyData;

    }

    public String[][] formatJdyData(String str) {
        // 去掉字符串中的空格
        str = str.replaceAll("\\s+","");

        // 去掉每个元素的方括号，并将逗号分隔的元素转换为数组
        String[] arr = str.substring(2, str.length() - 2).split("\\],\\[");

        // 将每个一维数组元素转换为Java数组
        String[][] result = new String[arr.length][];
        for (int i = 0; i < arr.length; i++) {
            String[] temp = arr[i].split(",");
//            for (int j = 0; j < temp.length; j++) {
//                temp[j] = temp[j].replaceAll("\"", "");
//            }
            result[i] = temp;
        }

        // 使用循环遍历访问每个元素
        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < result[i].length; j++) {
                System.out.print(result[i][j] + " ");
            }
            System.out.println();
        }
        return result;
    }

    public void createCYData(String jsonData) {
        // 1. 组装数据验证
        // 2. 考虑重试场景数据判重问题
        // 3. 重试机制思考
        // 4.
        if (jsonData == null) {
            jsonData = "{\"F0000029\":\"供应商-测试\",\"F0000031\":\"2023/7/7 0:00:00\",\"F0000032\":\"7f0ec4f5-9ce6-4abb-9a95-35eb1635d08c\",\"F0000013\":\"订单号-测试\",\"F0000026\":\"合同号-测试\",\"F0000017\":\"产品名称-测试\",\"F0000021\":\"物料名称-测试\",\"F0000024\":66,\"F0000022\":\"辅料规格-测试\",\"F0000028\":\"2023/7/11 0:00:00\",\"F0000030\":\"补充备注说明-测试\",\"F0000025\":[\"e5c4f77d-7d37-4154-9403-0b8d4b5858d9\",\"f4efea6c-4cfa-4c05-a3ab-76c75cf0e718\"],\"CreatedBy\":\"858c3e2a-b79f-410e-96eb-8e74c3b526d8\",\"CreatedTime\":\"2023/7/7 16:16:16\",\"OwnerId\":\"858c3e2a-b79f-410e-96eb-8e74c3b526d8\"}";
        }
        // 验证可行的测试数据，用于辅料采购单创建测试
//        jsonData = "{\"F0000029\":\"供应商-测试\",\"F0000031\":\"2023/7/7 0:00:00\",\"F0000032\":\"7f0ec4f5-9ce6-4abb-9a95-35eb1635d08c\",\"F0000013\":\"订单号-测试\",\"F0000026\":\"合同号-测试\",\"F0000017\":\"产品名称-测试\",\"F0000021\":\"物料名称-测试\",\"F0000024\":66,\"F0000022\":\"辅料规格-测试\",\"F0000028\":\"2023/7/11 0:00:00\",\"F0000030\":\"补充备注说明-测试\",\"F0000025\":[\"e5c4f77d-7d37-4154-9403-0b8d4b5858d9\",\"f4efea6c-4cfa-4c05-a3ab-76c75cf0e718\"],\"CreatedBy\":\"858c3e2a-b79f-410e-96eb-8e74c3b526d8\",\"CreatedTime\":\"2023/7/7 16:16:16\",\"OwnerId\":\"858c3e2a-b79f-410e-96eb-8e74c3b526d8\"}";

        String url = "https://www.h3yun.com/OpenApi/Invoke";
        Map<String, String> paramMap = new HashMap();
        paramMap.put("ActionName", "CreateBizObject");
        paramMap.put("SchemaCode", "D279357Sh1hjskmjufjwh8e3volp6rud7");
        paramMap.put("BizObject", jsonData);
        paramMap.put("IsSubmit", "true");
        //身份认证参数
        Map headers = new HashMap();
        headers.put("EngineCode","iskos7m6c2f9gp0wqcn7iel66");
        headers.put("EngineSecret","BFqZ8M35luiHgg3vwd46rytkkQP38s2LJWro0EG3oLfyiKKIUi6XGg==");
        headers.put("Content-Type","application/json");
        Gson gson = new Gson();
        System.out.println("gson.toJson(paramMap): " + gson.toJson(paramMap));
        String result = HttpRequestUtil.sendPost(url,   gson.toJson(paramMap), headers);
        System.out.println("result: " + result);
    }

    @Test
    public void queryCYData() {
        String url = "https://www.h3yun.com/OpenApi/Invoke";
        Map<String, String> paramMap = new HashMap();
        paramMap.put("ActionName", "LoadBizObject");
        paramMap.put("SchemaCode", "xgawkup5u6xm5xr0f4ylz37q0");
        paramMap.put("BizObjectId","85758cb9-f84a-415b-9263-ec86ef1f8df1");
//身份认证参数.
        Map headers = new HashMap();
        headers.put("EngineCode","iskos7m6c2f9gp0wqcn7iel66");
        headers.put("EngineSecret","BFqZ8M35luiHgg3vwd46rytkkQP38s2LJWro0EG3oLfyiKKIUi6XGg==");
        headers.put("Content-Type","application/json");

        Gson gson = new Gson();
        String result = HttpRequestUtil.sendPost(url,   gson.toJson(paramMap), headers);

        System.out.println("result: " + result);
    }

    public String queryCYDatas(String schemaCode, String filter) {
        String url = "https://www.h3yun.com/OpenApi/Invoke";
        Map<String, String> paramMap = new HashMap();
        paramMap.put("ActionName", "LoadBizObjects");
        paramMap.put("SchemaCode", schemaCode);
        paramMap.put("Filter", filter);
        //身份认证参数.
        Map headers = new HashMap();
        headers.put("EngineCode","iskos7m6c2f9gp0wqcn7iel66");
        headers.put("EngineSecret","BFqZ8M35luiHgg3vwd46rytkkQP38s2LJWro0EG3oLfyiKKIUi6XGg==");
        headers.put("Content-Type","application/json");

        Gson gson = new Gson();
        String result = HttpRequestUtil.sendPost(url, gson.toJson(paramMap), headers);

        System.out.println("result: " + result);
        return result;
    }

    /*本接口用于实现采购订单 (PUR_PurchaseOrder)的删除功能*/
    @Test
    public void itestdeletePurchaseOrder() throws Exception {
        JsonObject jsonData = new JsonObject();
        jsonData.addProperty("Numbers", FNumber);
        String result = api.delete("PUR_PurchaseOrder", jsonData.toString());
        Gson gson = new Gson();
        RepoRet repoRet = gson.fromJson(result, RepoRet.class);
        if (repoRet.getResult().getResponseStatus().isIsSuccess()) {

            System.out.printf("采购订单删除接口: %s%n", gson.toJson(repoRet.getResult().getResponseStatus()));
        } else {
            fail("采购订单删除接口: " + gson.toJson(repoRet.getResult().getResponseStatus()));
        }
    }

    /*本接夺口用于实现采购订单 (PUR_PurchaseOrder)的单据查询功能*/
    @Test
    public void queryJdyScdd() throws Exception {
        String FNumber = "230492";
        //0-5 供应商, 采购日期, 采购员, 单据编号, 采购订单号
        //5-5 客户合同号, 物料名称, 采购数量, 规格型号, 交货日期
        //10-4 补充说明备注, 创建人, 创建日期, 产品名称
        //14-6 实体主键, 单据状态, 朔翔号（订单号）, 备注1（辅料规格; 先占位）, 对应产品（产品名称）, 跟单员名称（业务员）
        //20 纸箱纸质要求（F_QWE_TEXT）, 使用说明（F_QWE_TEXT2）, 备注（FEntryNote）,
        // 最后： FModifyDate  ($$FAUXPROPID__FF100002,)
        // [[101527, 2023-07-06T00:00:00, 0, CGDD007360, null, 1TSGW8, null, 270048.0, 张 结算, 2023-07-10T00:00:00, 289139, 2023-07-05T17:54:50.953, 107592, B]]
        String fieldstr = "FID, F_qwe_Base1, F_qwe_Text4, F_qwe_Text6, F_qwe_Date16, F_qwe_Base3, F_qwe_Decimal6, F_qwe_Text10, F_qwe_Decimal7, F_qwe_Base.fname, F_qwe_Text16,F_qwe_BaseProperty1,  F_qwe_Text17,F_qwe_Decimal";
        String json = "{\n" +
                "    \"FormId\": \"k1583e5bde51d45a299c0126e98951888\",\n" +
                "    \"FieldKeys\": \"" + fieldstr + "\",\n" +
//                "    \"FilterString\": \"FBillNo=\'" + FNumber + "\' and FDocumentStatus=\'C\'\",\n" +
                "    \"FilterString\": \"F_qwe_Text6=\'" + FNumber + "\'\",\n" +
                "    \"OrderString\": \"\",\n" +
                "    \"TopRowCount\": 0,\n" +
                "    \"StartRow\": 0,\n" +
                "    \"Limit\": 0,\n" +
                "    \"SubSystemId\": \"\"\n" +
                "}";
        String result = String.valueOf(api.executeBillQuery(json));
//        if (result.length() > 2) {
//            String[][] formatResult = formatJdyData(result);
//            for (int i=0; i<formatResult.length; i++) {
//                createCYData(formatCyData(formatResult[i]));
//            }
//            System.out.println("formatResult: " + formatResult);
//        }
        System.out.println("生产订单单据查询接口: " + result);
    }

}
